#include <htc.h>
#include <stdio.h>
#include "usart.h"
//#include "cuscostdio.h"

#define _XTAL_FREQ 20000000
#define TELEFONE "92314502"

void 
putch(unsigned char byte) 
{
	/* output one byte */
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = byte;
}

unsigned char 
getch() {
	// retrieve one byte 
	while(!RCIF)	//set when register is not empty 
		continue;
	return RCREG;	
}

unsigned char
getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}

void envia_mensagem(char msg []){
	printf("At+CSQ\r");
	__delay_ms(3000);
	printf("At+CREG?\r");
	__delay_ms(3000);
	printf("At+CGATT?\r");
	__delay_ms(3000);
	printf("At+CMGF=1\r");
	__delay_ms(3000);
	printf("At+CSCS=\"GSM\"\r");
	__delay_ms(3000);
	printf("At+CMGS=\"%s\"\r", TELEFONE);
	__delay_ms(3000);
	printf("%s", msg);
	__delay_ms(3000);
	printf("\x1A"); //CTRL+Z
	__delay_ms(3000);
}

int check_msg() {

}

//Colocar o modulo GPRS em modo de economia de energia (sleep). Enquanto o modulo estiver no modo sleep a porta serial nao estara acessivel
void sleep_gprs(){
	//Comando para habilitar o modo sleep. O padrao eh igual a 0
	printf("At+CSCLK=1\r");
	__delay_ms(3000);
	printf("At+CFUN=0\r");
	__delay_ms(3000);
}

//
void wakeUp_grps(){
	
}

char * get_msg(char * msg){
	printf("AT+CMGF=1\r");
	__delay_ms(1000);
	printf("AT+CMGR=1\r");
	__delay_ms(1000);
	scanf("%s",&msg);
}
